package com.caotv.wkreport.dbutils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import com.caotv.wkreport.dao.User;
import com.caotv.wkreport.utils.LoggingUtils;

public class UserTableTool {

	public SessionFactory sessionFactory;

	public static UserTableTool getInstance() {
		UserTableTool userTableTool = new UserTableTool();
		userTableTool.sessionFactory = HibernateUtil.getSessionFactory();
		return userTableTool;
	}

	public User getUserByUsername(String username) {
		try {

			Session session = sessionFactory.openSession();
			User user = (User) session.createCriteria(User.class)
					.add(Restrictions.eq("username", username)).uniqueResult();
			session.close();
			return user;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get user by username");
			ex.printStackTrace();
			return null;
		}
	}

	public List<User> getAllUsers() {
		List<User> list = new ArrayList<User>();
		try {
			Session session = sessionFactory.openSession();
			list = session.createCriteria(User.class).list();
			session.close();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get all user");
			ex.printStackTrace();
		}
		return list;
	}

	public User getUserById(int id) {
		try {
			Session session = sessionFactory.openSession();
			User user = (User) session.createCriteria(User.class)
					.add(Restrictions.eq("id", id)).uniqueResult();
			session.close();
			return user;
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when get user by username");
			ex.printStackTrace();
			return null;
		}
	}

	public void deleteUserById(int id) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			User user = getUserById(id);
			session.delete(user);
			trans.commit();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when delete user by user id");
			trans.rollback();
			ex.printStackTrace();
		}
		session.close();
	}

	public boolean updateUser(User user) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			session.saveOrUpdate(user);
			trans.commit();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when update user");
			trans.rollback();
			ex.printStackTrace();
			return false;
		}
		session.close();
		return true;
	}
	
	public boolean addUser(User user) {
		Session session = sessionFactory.openSession();
		Transaction trans = session.beginTransaction();
		try {
			session.save(user);
			trans.commit();
		} catch (Exception ex) {
			LoggingUtils.getInstance().log(Level.WARNING,
					"Error when update user");
			trans.rollback();
			ex.printStackTrace();
			return false;
		}
		session.close();
		return true;
	}
}
